/* eslint-disable no-param-reassign */
import { getSearchResult } from '../../../services/good/fetchSearchResult';
import Toast from 'tdesign-miniprogram/toast/index';

const initFilters = {
	overall: 1,
	sorts: '',
};

Page({
	data: {
		goodsList: [],
		sorts: '',
		overall: 1,
		show: false,
		minVal: '',
		maxVal: '',
		minSalePriceFocus: false,
		maxSalePriceFocus: false,
		filter: initFilters,
		hasLoaded: false,
		keywords: '',
		loadMoreStatus: 0,
		loading: true,
	},

	total: 0,
	page: 0,
	pageSize: 20,

	onLoad(options) {
		const { searchValue = '' } = options || {};
		this.setData(
			{
				keywords: searchValue,
			},
			() => {
				this.init(true);
			}
		);
	},

	generalQueryData(reset = false) {
		const { filter, keywords, minVal, maxVal } = this.data;
		const { page, pageSize } = this;
		const { sorts, overall } = filter;
		const params = {
			sort: 0, // 0 综合，1 价格
			page: 0,
			pageSize: 20,
			keyword: keywords,
		};

		if (sorts) {
			params.sort = 1;
			params.sortType = sorts === 'desc' ? 1 : 0;
		} else {
			params.sort = 0;
		}
		params.minPrice = minVal ? minVal * 100 : 0;
		params.maxPrice = maxVal ? maxVal * 100 : '';
		if (reset) return params;
		return {
			...params,
			page: page + 1,
			pageSize,
		};
	},

	async init(reset = true) {
		const { loadMoreStatus, goodsList = [] } = this.data;
		const params = this.generalQueryData(reset);
		if (loadMoreStatus !== 0) return;
		this.setData({
			loadMoreStatus: 1,
			loading: true,
		});
		try {
			const result = await getSearchResult(params);
			const data = result;
			const { spuList, totalCount = 0 } = data;
			if (totalCount === 0 && reset) {
				this.total = totalCount;
				this.setData({
					emptyInfo: {
						tip: '抱歉，未找到相关商品',
					},
					hasLoaded: true,
					loadMoreStatus: 0,
					loading: false,
					goodsList: [],
				});
				return;
			}
			const _goodsList = reset ? spuList : goodsList.concat(spuList);
			_goodsList.forEach((v) => {
				if (v.spuTagList) {
					v.tags = v.spuTagList.map((u) => u.title);
				}
				v.hideKey = { desc: true };
			});
			const _loadMoreStatus = _goodsList.length === totalCount ? 2 : 0;
			this.page = params.page || 1;
			this.total = totalCount;
			this.setData({
				goodsList: _goodsList,
				loadMoreStatus: _loadMoreStatus,
			});
		} catch (error) {
			this.setData({
				loading: false,
			});
		}
		this.setData({
			hasLoaded: true,
			loading: false,
		});
	},

	handleCartTap() {
		wx.switchTab({
			url: '/pages/cart/index',
		});
	},

	handleSubmit() {
		this.setData(
			{
				goodsList: [],
				loadMoreStatus: 0,
			},
			() => {
				this.init(true);
			},
		);
	},

	onReachBottom() {
		const { goodsList } = this.data;
		const { total = 0 } = this;
		if (goodsList.length === total) {
			this.setData({
				loadMoreStatus: 2,
			});
			return;
		}
		this.init(false);
	},

	handleAddCart() {
		Toast({
			context: this,
			selector: '#t-toast',
			message: '点击加购',
		});
	},

	gotoGoodsDetail(e) {
		const { index } = e.detail;
		const { spuId } = this.data.goodsList[index];
		wx.navigateTo({
			url: `/pages/goods/details/index?spuId=${spuId}`,
		});
	},

	handleFilterChange(e) {
		const { overall, sorts } = e.detail;
		const { total } = this;
		const _filter = {
			sorts,
			overall,
		};
		this.setData({
			filter: _filter,
			sorts,
			overall,
		});

		this.page = 1;
		this.setData(
			{
				goodsList: [],
				loadMoreStatus: 0,
			},
			() => {
				total && this.init(true);
			},
		);
	},

	showFilterPopup() {
		this.setData({
			show: true,
		});
	},

	showFilterPopupClose() {
		this.setData({
			show: false,
		});
	},

	onMinValAction(e) {
		const { value } = e.detail;
		this.setData({ minVal: value });
	},

	onMaxValAction(e) {
		const { value } = e.detail;
		this.setData({ maxVal: value });
	},

	reset() {
		this.setData({ minVal: '', maxVal: '' });
	},

	confirm() {
		const { minVal, maxVal } = this.data;
		this.page = 0;
		this.setData(
			{
				show: false,
				minVal: minVal,
				goodsList: [],
				loadMoreStatus: 0,
				maxVal: maxVal,
			},
			() => {
				this.init();
			},
		);
	},
});
